package ai.loa;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import ai.loa.ai.LOAAIPlayer;
import ai.loa.ai.RemotePlayer;
import ai.loa.framework.LOABoard;
import ai.loa.framework.controller.LOAController;
import ai.loa.framework.controller.LocalLOAController;
import ai.loa.framework.view.LOAImageFactory;
import ai.loa.framework.view.SwingLOAView;
import ai.loa.utility.IndependentFeatures;
import ai.loa.utility.TraspositionTables;

public class LOAMain {
	public static void main(String[] args) throws Exception {
		// System.setOut(new PrintStream(new File(
		// "C:/Users/Giuseppe/Desktop/prova.txt")));
		Class.forName("ai.loa.ai.Mossa");
		Class.forName("ai.loa.utility.ConstantValues");
		Class.forName("ai.loa.Parameters");
		Class.forName("ai.loa.utility.IndependentFeatures");
		TraspositionTables.getInstance();
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		LOAImageFactory factory = new LOAImageFactory("default");

		int locale = JOptionPane.showConfirmDialog(frame,
				"Vuoi giocare in locale ?", "", JOptionPane.YES_NO_OPTION);

		if (locale == JOptionPane.NO_OPTION) {
			String colore = "";
			Object playerColour = JOptionPane.showInputDialog(null,
					"Giocatore", "", JOptionPane.QUESTION_MESSAGE, null,
					new Object[] { "Bianco", "Negro" }, 0);
			if (playerColour == null) {
				return;
			}
			colore = (String) playerColour;
			frame.setTitle(colore);
			Object port = JOptionPane.showInputDialog(null, "Inserire porta",
					50001);
			int porta = Integer.parseInt((String) port);

			if (colore.equals("Bianco")) {
				ServerSocket ss = new ServerSocket(porta);
				Socket s = ss.accept();

				LOAController controller = new LocalLOAController(
						LOABoard.PLAYER_1);

				LOAAIPlayer ai = new LOAAIPlayer(controller);
				controller.setObserver(ai, LOABoard.PLAYER_1);// setta se stesso
																// come
				// giocatore partita
				SwingLOAView view = new SwingLOAView(factory, controller.stato,
						controller);
				RemotePlayer rp = new RemotePlayer(s, controller);
				controller.setObserver(rp, LOABoard.PLAYER_2);
				rp.start();
				frame.setLayout(new BorderLayout());

				frame.add(view, BorderLayout.CENTER);
				frame.pack();
				frame.setVisible(true);
				controller.startGame();
			} else {
				Object address = JOptionPane.showInputDialog(null,
						"Inserire host", "localhost");
				Socket s = new Socket(address.toString(), porta);

				LOAController controller = new LocalLOAController(
						LOABoard.PLAYER_1);

				LOAAIPlayer ai = new LOAAIPlayer(controller);
				controller.setObserver(ai, LOABoard.PLAYER_2);// setta se stesso
																// come
				// giocatore partita
				SwingLOAView view = new SwingLOAView(factory, controller.stato,
						controller);
				RemotePlayer rp = new RemotePlayer(s, controller);
				controller.setObserver(rp, LOABoard.PLAYER_1);
				rp.start();
				frame.setLayout(new BorderLayout());

				frame.add(view, BorderLayout.CENTER);
				frame.pack();
				frame.setVisible(true);
				controller.startGame();
			}

		} else {
			String colore = "";
			Object playerColour = JOptionPane.showInputDialog(null,
					"Giocatore", "", JOptionPane.QUESTION_MESSAGE, null,
					new Object[] { "Bianco", "Negro" }, 0);
			if (playerColour == null) {
				return;
			}
			colore = (String) playerColour;
			frame.setTitle(colore);
			if (colore.equals("Negro")) {
				LOAController controller = new LocalLOAController(
						LOABoard.PLAYER_1);
				LOAAIPlayer ai = new LOAAIPlayer(controller);
				// come
				// giocatore partita
				controller.setObserver(ai, LOABoard.PLAYER_1);
				SwingLOAView view = new SwingLOAView(factory, controller.stato,
						controller);
				controller.setObserver(view, LOABoard.PLAYER_2);
				frame.setLayout(new BorderLayout());
				frame.add(view, BorderLayout.CENTER);
				frame.pack();
				frame.setVisible(true);
				controller.startGame();
			} else {
				LOAController controller = new LocalLOAController(
						LOABoard.PLAYER_2);
				LOAAIPlayer ai = new LOAAIPlayer(controller);
				// come
				// giocatore partita
				SwingLOAView view = new SwingLOAView(factory, controller.stato,
						controller);
				controller.setObserver(view, LOABoard.PLAYER_1);
				controller.setObserver(ai, LOABoard.PLAYER_2);
				frame.setLayout(new BorderLayout());
				frame.add(view, BorderLayout.CENTER);
				frame.pack();
				frame.setVisible(true);
			}
			Frame f = new Frame();
			JButton b = new JButton("Save");
			b.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					IndependentFeatures.save();
				}
			});
			f.add(b);
			f.pack();
			f.setVisible(true);
		}
	}
}
